package com.cn.hangyun.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cn.hangyun.common.CommonKeys;
import com.cn.hangyun.common.DateUtil;
import com.cn.hangyun.dao.Rt_accelration_controlMapper;
import com.cn.hangyun.dao.Rt_accelration_dataMapper;
import com.cn.hangyun.dao.Rt_accelration_transducerMapper;
import com.cn.hangyun.dao.Rt_strain_controlMapper;
import com.cn.hangyun.dao.Rt_strain_dataMapper;
import com.cn.hangyun.dao.Rt_strain_transducerMapper;
import com.cn.hangyun.model.Rt_accelration_control;
import com.cn.hangyun.model.Rt_accelration_transducer;
import com.cn.hangyun.model.Rt_strain_control;
import com.cn.hangyun.model.Rt_strain_data;
import com.cn.hangyun.model.Rt_strain_transducer;
import com.cn.hangyun.model.requestVO.AccelrationRequestBean;
import com.cn.hangyun.model.requestVO.StrainRequestBean;
import com.cn.hangyun.service.IModelService;

@Service("modelService")
public class ModelServiceImpl implements IModelService {

	@Autowired
	private Rt_strain_controlMapper strContMapper;
	
	@Autowired
	private Rt_strain_transducerMapper strTranMapper;
	
	@Autowired
	private Rt_strain_dataMapper strDataMapper;
	
	@Autowired
	private Rt_accelration_controlMapper accContMapper;
	
	@Autowired
	private Rt_accelration_transducerMapper accTranMapper;
	
	@Autowired
	private Rt_accelration_dataMapper accDataMapper;
	

	@Override
	public Rt_accelration_control getLastAccelrationControl(Date current) {
		
		Rt_accelration_control data = accContMapper.selectLast(current);
		return data;
	}

	@Override
	public Rt_strain_control getLastStrainControl(Date current) {
		
		Rt_strain_control data = strContMapper.selectLast(current);
		return data;
	}

	@Override
	public List<Rt_accelration_transducer> getAllAccTran() {
		List<Rt_accelration_transducer> list = accTranMapper.selectAll();
		return list;
	}

	@Override
	public Map<String, List<String>> getAccData(String controlId, Date start, Date end,
			String[] tranList) {
		
		Map<String, List<String>> dataMap = new HashMap<String, List<String>>();
		for (String trans : tranList) {
			List<String> list = accDataMapper.selectData(controlId, start, end, trans);
			dataMap.put(trans, list);
		}
		return dataMap;
	}

	@Override
	public List<Rt_strain_transducer> getAllStrTran() {
		List<Rt_strain_transducer> list = strTranMapper.selectAll();
		return list;
	}

	@Override
	public Map<String, List<String>> getAccDataMore(AccelrationRequestBean bean) {
		
		Date start = bean.getStart();
	    Date end = DateUtil.getEndTime(start, bean.getInterval());
	    String controlId = bean.getControlId();
	    String tran1 = bean.getTran1();
	    String tran2 = bean.getTran2();
	    String tran3 = bean.getTran3();
		
		String direct1 = bean.getDirection1();
		List<String> data1 = null;
		if (direct1.equals(CommonKeys.DIRECTION_X)) {
			data1 = accDataMapper.selectDataX(controlId, start, end, tran1);
		} else if (direct1.equals(CommonKeys.DIRECTION_Y)){
			data1 = accDataMapper.selectDataY(controlId, start, end, tran1);
		} else {
			data1 = accDataMapper.selectDataZ(controlId, start, end, tran1);
		}
		
		
		String direct2 = bean.getDirection2();
		List<String> data2 = null;
		if (direct2.equals(CommonKeys.DIRECTION_X)) {
			data2 = accDataMapper.selectDataX(controlId, start, end, tran2);
		} else if (direct2.equals(CommonKeys.DIRECTION_Y)){
			data2 = accDataMapper.selectDataY(controlId, start, end, tran2);
		} else {
			data2 = accDataMapper.selectDataZ(controlId, start, end, tran2);
		}
		
		String direct3 = bean.getDirection2();
		List<String> data3 = null;
		if (direct3.equals(CommonKeys.DIRECTION_X)) {
			data3 = accDataMapper.selectDataX(controlId, start, end, tran3);
		} else if (direct3.equals(CommonKeys.DIRECTION_Y)){
			data3 = accDataMapper.selectDataY(controlId, start, end, tran3);
		} else {
			data3 = accDataMapper.selectDataZ(controlId, start, end, tran3);
		}
		
		Map<String, List<String>> map = new LinkedHashMap<String, List<String>>();
		map.put(tran1, data1);
		map.put(tran2, data2);
		map.put(tran3, data3);
		
		return map;
	}

	@Override
	public Map<String, List<List<String>>> getStrDataMore(StrainRequestBean bean) {
		
		Date start = bean.getStart();
	    Date end = DateUtil.getEndTime(start, bean.getInterval());
	    String controlId = bean.getControlId();
		
		List<String> data11 = strDataMapper.selectData(controlId, start, end, bean.getTran11());
		List<String> data12 = strDataMapper.selectData(controlId, start, end, bean.getTran12());
		List<String> data21 = strDataMapper.selectData(controlId, start, end, bean.getTran21());
		List<String> data22 = strDataMapper.selectData(controlId, start, end, bean.getTran22());
		List<String> data31 = strDataMapper.selectData(controlId, start, end, bean.getTran31());
		List<String> data32 = strDataMapper.selectData(controlId, start, end, bean.getTran32());
		
		List<List<String>> list1 = new ArrayList<List<String>>();
		list1.add(data11);
		list1.add(data12);
		
		List<List<String>> list2 = new ArrayList<List<String>>();
		list2.add(data21);
		list2.add(data22);
		
		List<List<String>> list3 = new ArrayList<List<String>>();
		list3.add(data31);
		list3.add(data32);
		
		
		Map<String, List<List<String>>> map = new LinkedHashMap<String, List<List<String>>>();
		map.put("first", list1);
		map.put("second", list2);
		map.put("third", list3);
	
		return map;
	}


}
